/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * DateAndTimeSelector.java
 *
 * Created on 02/05/2009, 01:08:05
 */
package gui;

import com.toedter.calendar.JDateChooser;
import com.toedter.calendar.JSpinnerDateEditor;
import java.awt.Dialog;
import java.awt.Frame;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.Insets;
import java.awt.event.ActionEvent;
import java.util.Calendar;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JLabel;
import javax.swing.JSpinner;
import javax.swing.SpinnerModel;
import javax.swing.SpinnerNumberModel;

/**
 *
 * @author Elad
 */
public class DateAndTimeSelector extends javax.swing.JDialog {

    private Calendar currentTime;
    private JDateChooser dateSelector;
    private JSpinner hour,  min,  sec;
    private JButton ok,  cancel;
    private JLabel date,  time;

    /** Creates new form DateAndTimeSelector */
    DateAndTimeSelector(Frame parent, String title, ModalityType modal, Calendar now) {
        super(parent, title, modal);
        currentTime = now;
        initComponents();

    }

    public static Calendar showDateAndTimeSelector(Frame frame, String title, Calendar c) {
        DateAndTimeSelector dateAndTime = new DateAndTimeSelector(frame, title, Dialog.ModalityType.DOCUMENT_MODAL, c);
        dateAndTime.setVisible(true);
        return dateAndTime.getSelectedDate();
    }

    private Calendar getSelectedDate() {
//        System.out.println("printing time");
        return currentTime;
    }

    private void addEntry(JComponent component, GridBagLayout grid, GridBagConstraints c) {
        /*JLabel label = new JLabel(text + ": ", null, JLabel.RIGHT);
        c.gridx--;
        grid.setConstraints(label, c);
        add(label);
        c.gridx++;*/
        grid.setConstraints(component, c);
        add(component);
    //  c.gridwidth = GridBagConstraints.REMAINDER;

    }

    private void initComponents() {

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        GridBagLayout gridbag = new GridBagLayout();
        GridBagConstraints c = new GridBagConstraints();
        c.weightx = 1.0;
        c.fill = GridBagConstraints.BOTH;
        setLayout(gridbag);
        Calendar now = currentTime;
        dateSelector = new JDateChooser(null, now.getTime(), null, new JSpinnerDateEditor()); 
        SpinnerModel minModel = new SpinnerNumberModel(now.get(Calendar.MINUTE), 0, 59, 1);
        SpinnerModel secModel = new SpinnerNumberModel(now.get(Calendar.SECOND), 0, 59, 1);
        SpinnerModel hourModel = new SpinnerNumberModel(now.get(Calendar.HOUR_OF_DAY), 0, 23, 1);
        hour = new JSpinner(hourModel);
        min = new JSpinner(minModel);
        sec = new JSpinner(secModel);
        ok = new JButton("OK");
        cancel = new JButton("Cancel");
        date = new JLabel("Date:");
        time = new JLabel("Time:");

        ok.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                okButtonActionPerformed(evt);
            }
        });
        cancel.addActionListener(new java.awt.event.ActionListener() {

            public void actionPerformed(java.awt.event.ActionEvent evt) {
                cancelButtonActionPerformed(evt);
            }
        });

        //arranging the components of the dialog
        c.gridx = 1;
        addEntry(date, gridbag, c);
        c.gridx++;
        c.gridwidth = 3;
        addEntry(dateSelector, gridbag, c); 
        c.gridy = 2;
        c.gridwidth = 1;
        c.gridx = 1;
        addEntry(time, gridbag, c);
        c.gridx++;
        addEntry(hour, gridbag, c);
        c.gridx++;
        addEntry(min, gridbag, c);
        c.gridx++;
        addEntry(sec, gridbag, c);
        c.gridy = 8;
        c.gridx++;
        c.insets = new Insets(20, 5, 5, 5);  //top padding
        addEntry(ok, gridbag, c);
        c.gridx = 0;
        addEntry(cancel, gridbag, c);
        pack();

    }

    private void okButtonActionPerformed(ActionEvent evt) {
        currentTime = dateSelector.getCalendar(); 
        currentTime.set(Calendar.HOUR_OF_DAY, (Integer) hour.getModel().getValue());
        currentTime.set(Calendar.MINUTE, (Integer) min.getModel().getValue());
        currentTime.set(Calendar.SECOND, (Integer) sec.getModel().getValue());
        //   SimpleDateFormat frmt =  new SimpleDateFormat("yyyy/MM/dd  hh:mm:ss");
        //System.out.println("current Time "+frmt.format(currentTime.getTime()));
        dispose();
    }

    private void cancelButtonActionPerformed(ActionEvent evt) {
        dispose();
    }

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                DateAndTimeSelector dialog = new DateAndTimeSelector(new javax.swing.JFrame(), "testing", Dialog.ModalityType.DOCUMENT_MODAL, Calendar.getInstance());
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {

                    @Override
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    // End of variables declaration//GEN-END:variables
}
